import Vue from 'vue'
import Vuex from 'vuex'
import url from './serviceAPI.config';
import axios from 'axios';

Vue.use(Vuex)
import { ADD_USER ,CHANGE_LOGING, ADD_NEWS, DEL_USER} from './mutation-types';
import { promises } from 'fs';

export default new Vuex.Store({
  state: {
    token:'',
    isLoging:false,
    userNames:[],
    movies:[
      {
        id:0,
        imgUrl: 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2535096871.webp',
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:1,
        imgUrl:'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2529701498.webp',
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:2,
        imgUrl:'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2534091010.webp',
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:3,
        imgUrl:'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2535726164.webp',
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:0,
        imgUrl: 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2535096871.webp',
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:1,
        imgUrl:'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2529701498.webp',
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:2,
        imgUrl:'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2534091010.webp',
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:3,
        imgUrl:'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2535726164.webp',
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:0,
        imgUrl: 'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2535096871.webp',
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:1,
        imgUrl:'https://img1.doubanio.com/view/photo/s_ratio_poster/public/p2529701498.webp',
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:2,
        imgUrl:'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2534091010.webp',
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:3,
        imgUrl:'https://img3.doubanio.com/view/photo/s_ratio_poster/public/p2535726164.webp',
        name:'@万斯',
        time:'2018-10-10'
      }
    ],
    imags:[
      {
        id:0,
        imgUrl: require('./assets/images/00.jpg'),
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:1,
        imgUrl:require('./assets/images/01.jpg'),
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:2,
        imgUrl:require('./assets/images/02.jpg'),
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:3,
        imgUrl:require('./assets/images/03.jpeg'),
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:4,
        imgUrl:require('./assets/images/04.jpeg'),
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:5,
        imgUrl:require('./assets/images/00.jpg'),
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:6,
        imgUrl:require('./assets/images/00.jpg'),
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:0,
        imgUrl: require('./assets/images/00.jpg'),
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:1,
        imgUrl:require('./assets/images/01.jpg'),
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:2,
        imgUrl:require('./assets/images/02.jpg'),
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:3,
        imgUrl:require('./assets/images/03.jpeg'),
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:4,
        imgUrl:require('./assets/images/04.jpeg'),
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:5,
        imgUrl:require('./assets/images/00.jpg'),
        name:'@万斯',
        time:'2018-10-10'
      },
      {
        id:6,
        imgUrl:require('./assets/images/00.jpg'),
        name:'@万斯',
        time:'2018-10-10'
      }
    ],
    news:[],
    articles:[
      {
        id:0,
        title:'王者0_article',
        author:'乌云0',
        content:'不急字啊你不能后阿斯哈佛is的哈佛ID收费机的设计费',
        time:new Date()
      },
      {
        id:1,
        title:'王者1_article',
        author:'乌云1',
        content:'不急字啊你不能后阿斯哈佛is的哈佛ID收费机的设计费',
        time:new Date()
      },
      {
        id:2,
        title:'王者2_article',
        author:'乌云2',
        content:'不急字啊你不能后阿斯哈佛is的哈佛ID收费机的设计费',
        time:new Date()
      }
    ]
  },
  mutations: {
     [ADD_USER] (state,userObj) {
      state.userNames = [ ...state.userNames, userObj ]
     },
     [CHANGE_LOGING] (state) {
       state.isLoging = true;
     },
     [ADD_NEWS] (state,obj) {
       state.news = [...state.news,obj]
     },
     [DEL_USER] (state,obj) {
      state.userNames.map((item,index)=>{
         if(item._id == obj){
          state.userNames.splice(index,1);
          return true;
         }
      })
     }
  },
  actions: {
    delUser({commit,state},item){
       return new Promise((resolve, reject)=>{
        axios.post(url.userDelApi, item)
        .then(function (response) {
             commit('DEL_USER',item._id)  
             resolve()
        })
        .catch(function (error) {
            console.log(error);
        });
       })
    },
    getAllUsers({commit,state}){
      axios.get(url.userGetApi)
      .then( (response)=> {
        state.userNames = response.data.data;
      })
      .catch( (error)=> {
        console.log(error);
      });
    },
    addContentToState({commit,state}){
      axios.get(url.contentGetApi)
      .then( (response)=> {
        state.news = response.data.data;
      })
      .catch( (error)=> {
        console.log(error);
      });
    },
    addContent ({ commit },contentData) {
      return new Promise((resolve, reject) => {
        axios.post(url.contentPostApi, contentData)
          .then(function (response) {
               commit('ADD_NEWS',contentData)  
               resolve()
          })
          .catch(function (error) {
              console.log(error);
          });
        
      })
    }
  }
})
